<?php
/**
 *
 * 动态判断下载权限
 *
 */
require_once(dirname(__FILE__)."/../include/common.inc.php");
require_once(DEDEINC."/memberlogin.class.php");
$ml = new MemberLogin();
$muid = $ml->M_ID;
$aid = intval($aid);
//查询模板所需要的金币
$moxingid = $dsql->GetOne("SELECT channel,mid FROM `#@__archives` WHERE id='$aid'");
$zuozhe = $moxingid['mid'];
$moxingid =$moxingid[channel];
$cts = $dsql->GetOne("SELECT addtable From `#@__channeltype` WHERE id='$moxingid' ");
$addtable = trim($cts['addtable']);
$ggg = $dsql->GetOne("SELECT * FROM `$addtable` WHERE aid='$aid'");
$needmoney = $ggg['needmoney'];
//查询会员所拥有的金币
$money = $dsql->GetOne("SELECT * FROM `#@__member` WHERE mid='$muid'");
$money = $money['money'];
$d = $dsql->GetOne("select mid from #@__member_operation where buyid='ARCHIVE".$aid."' And mid='$muid'");
//转换获取下载地址
$dowurl = "$cfg_phpurl/download.php?open=0&aid=$aid";

//如果是免费素材
if($needmoney==0 && $muid!=0)
{
	$msg = "该素材为免费资源,享免费特权";
	$anniu = "<a id='pay-btn' href='".$dowurl."' target='_blank' class='join-btn gradient-hor-og graHover'>免费下载</a>";
	$arr = array(
	     "status"=>"4",//免费
		 "msg"=>$msg,
		 "anniu"=>$anniu
    );
    echo json_encode($arr);
    exit;
}
//如果是管理员
else if($ml->isAdmin==1)
{
	$msg = "您是管理员，享免费特权";
	$anniu = "<a id='pay-btn' href='".$dowurl."' target='_blank' class='join-btn gradient-hor-og graHover'>免费下载</a>";
	$arr = array(
	     "status"=>"5",//免费
		 "msg"=>$msg,
		 "anniu"=>$anniu
    );
    echo json_encode($arr);
    exit;
}
//如果是该资源的作者
else if($zuozhe==$muid)
{
	$msg = "您是该资源的作者，享免费特权";
	$anniu = "<a id='pay-btn' href='".$dowurl."' target='_blank' class='join-btn gradient-hor-og graHover'>免费下载</a>";
	$arr = array(
	     "status"=>"6",//免费
		 "msg"=>$msg,
		 "anniu"=>$anniu
    );
    echo json_encode($arr);
    exit;
}
//如果会员已经登录
else if($muid!=0){   
   //如果没有下载过
   if($d['mid'] != $muid)
   {
		//如果金币不足 显示充值按钮
		if($money < $needmoney)
		{
			$msg = "您的余额不足，请先充值再试！";
			$anniu = "<a id='pay-btn' href='/member/buy.php' target='_blank' class='join-btn gradient-hor-og graHover'>需要充值".$needmoney."元</a>";
			$arr = array(
	           "status"=>"1",//余额不足
			   "msg"=>$msg,
			   "anniu"=>$anniu
            );
            echo json_encode($arr);
            exit;
		}
		//金币足够本次下载 直接显示下载
		else
		{
			$msg = "本次下载需要".$needmoney."元";
			$anniu = "<a id='pay-btn' href='".$dowurl."' target='_blank' class='join-btn gradient-hor-og graHover'>确认下载</a>";
			$arr = array(
	           "status"=>"2",//正常扣费下载
			   "msg"=>$msg,
			   "anniu"=>$anniu
            );
            echo json_encode($arr);
            exit;
		}
	}
	//如果有下载过 则提示不重复扣费
	else
	{
		$msg = "您已经购买过该素材，享免费特权";
		$anniu = "<a id='pay-btn' href='".$dowurl."' target='_blank' class='join-btn gradient-hor-og graHover'>免费下载</a>";
		$arr = array(
	         "status"=>"3",//正常扣费下载
			 "msg"=>$msg,
			 "anniu"=>$anniu
        );
        echo json_encode($arr);
        exit;
	}
}else{
   $arr = array(
	   "status"=>"0"//未登录
   );
   echo json_encode($arr);
   exit;
}
?>